/**
 *
 */
package org.shan.security.core.social.mlnx.api;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

/**
 * @author zhailiang
 *
 */
public class MlnxImpl extends AbstractOAuth2ApiBinding implements Mlnx {

	private Logger logger = LoggerFactory.getLogger(MlnxImpl.class);

	// 根据access_token获取USERINFO
	private static final String URL_GET_USERINFO = "http://localhost:8080/data";


	private ObjectMapper objectMapper = new ObjectMapper();

	public MlnxImpl(String accessToken, String appId) {

		// 默认是把accessToken放入请求头中
		// qqapi的文档确是放在参数中传递的
		super(accessToken, TokenStrategy.ACCESS_TOKEN_PARAMETER);
	}

	/* (non-Javadoc)
	 * @see com.imooc.security.core.social.qq.api.Ali#getUserInfo()
	 */
	@Override
	public MlnxUserInfo getUserInfo() {

		String result = getRestTemplate().getForObject(URL_GET_USERINFO, String.class);

		logger.info(result);

		MlnxUserInfo userInfo = null;
		try {
			userInfo = objectMapper.readValue(result, MlnxUserInfo.class);
			return userInfo;
		} catch (Exception e) {
			throw new RuntimeException("获取用户信息失败", e);
		}
	}

}
